package com.aidong.web.controller;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.wills.DicAdmAreaCodeDao;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.SysMenuDao;
import com.aidong.dao.wills.SysUserDao;
import com.aidong.dao.wills.SysUserGroupDao;
import com.aidong.model.LoginUser;
import com.aidong.model.MenuAuthority;
import com.aidong.model.RequestInfo;
import com.aidong.model.wills.DicAdmAreaCode;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysMenu;
import com.aidong.model.wills.SysUser;
import com.aidong.model.wills.SysUserGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController extends BaseController {
    private static final int BASE_MOD_ID = 0;
    @Resource
    SysUserGroupDao sysUserGroupDao;
    @Resource
    SysMenuDao sysMenuDao;
    @Resource
    SysUserDao sysUserDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    DicAdmAreaCodeDao dicAdmAreaCodeDao;

    public IndexController() {
    }

    @RequestMapping({"/"})
    public ModelAndView index() {
        List shopList;
        if(this.userSession.getLoginUser().getUserInfo().getLoginAllShop().intValue() == 1) {
            shopList = this.dicShopCodeDao.getAllShopList();
        } else {
            shopList = this.sysUserDao.getUserShopList(this.getLoginUserCode(), false);
        }

        return (new ModelAndView("index")).addObject("shopList", shopList);
    }

    @RequestMapping({"restrict"})
    public ModelAndView restrict() {
        return this.showAlert("没有权限");
    }

    @RequestMapping({"getMenu"})
    @ResponseBody
    public String getMenu(HttpServletRequest request) {
        List allList = this.sysMenuDao.getMenuAuthorityList();
        LinkedHashMap allMap = new LinkedHashMap();
        Iterator req = allList.iterator();

        while(req.hasNext()) {
            MenuAuthority user = (MenuAuthority)req.next();
            allMap.put(user.getMenuId(), user);
        }

        RequestInfo var21 = this.userSession.getRequestInfo();
        LoginUser var22 = this.userSession.getLoginUser();
        MenuAuthority map1;
        if(!this.chkNull(var22.getUserInfo().getGroupCode())) {
            SysUserGroup ls1 = (SysUserGroup)this.sysUserGroupDao.getById(var22.getUserInfo().getGroupCode());
            if(ls1 != null && ("1".equals(ls1.getSecuityLevel()) || var21 != null && var21.chkAllowIp(ls1.getAllowIp()))) {
                List useList = this.sysMenuDao.getGroupAuthorityMenuList(ls1.getGroupCode());
                Iterator lssc = useList.iterator();

                while(lssc.hasNext()) {
                    map1 = (MenuAuthority)lssc.next();
                    MenuAuthority parentMap = (MenuAuthority)allMap.get(map1.getMenuId());
                    if(parentMap != null) {
                        parentMap.setAuthority(parentMap.getAuthority() | map1.getAuthority());
                    }
                }
            }
        }

        List var23 = this.sysMenuDao.getUserAuthorityMenuList(var22.getUserCode());
        Iterator var24 = var23.iterator();

        while(var24.hasNext()) {
            MenuAuthority var26 = (MenuAuthority)var24.next();
            map1 = (MenuAuthority)allMap.get(var26.getMenuId());
            if(map1 != null) {
                map1.setAuthority(var26.getAuthority());
            }
        }

        ArrayList var25 = new ArrayList();
        List var27 = this.sysMenuDao.getShortcutMenuList();
        Iterator var28 = var27.iterator();

        while(var28.hasNext()) {
            SysMenu var30 = (SysMenu)var28.next();
            MenuAuthority authMap = (MenuAuthority)allMap.get(var30.getMenuId());
            if(authMap != null && authMap.getAuthority() > 0) {
                var25.add(authMap);
            }
        }

        HashMap var29 = new HashMap();
        HashMap var31 = new HashMap();
        HashMap var32 = new HashMap();
        ArrayList menuList = new ArrayList();
        MenuAuthority top = (MenuAuthority)allMap.get(Integer.valueOf(0));
        if(top != null) {
            menuList.add(top);
        }

        var31.put(Integer.valueOf(0), top);

        while(var31.size() > 0) {
            var29.clear();
            Iterator shopMenu = allList.iterator();

            while(shopMenu.hasNext()) {
                MenuAuthority shop = (MenuAuthority)shopMenu.next();
                if(shop.getAuthority() > 0 && var31.containsKey(shop.getParentId())) {
                    if(!this.chkNull(shop.getCodeName())) {
                        MenuAuthority au1 = (MenuAuthority)var32.get(shop.getCodeName());
                        if(au1 != null) {
                            shop.setAuthority(shop.getAuthority() | au1.getAuthority());
                            if(!this.chkNull(au1.getOperateEx())) {
                                shop.setOperateEx(au1.getOperateEx());
                            }
                        }

                        var32.put(shop.getCodeName(), shop);
                    }

                    menuList.add(shop);
                    var29.put(shop.getMenuId(), shop);
                }
            }

            var31.clear();
            var31.putAll(var29);
        }

        int var33 = 3;
        DicShopCode var34 = var22.getLoginShop();
        if(var34 != null) {
            var33 = "01".equals(var34.getShopType())?1:2;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("{menu:[");
        Iterator n = menuList.iterator();

        while(n.hasNext()) {
            MenuAuthority i$ = (MenuAuthority)n.next();
            if((i$.getMenuShow().intValue() & var33) == var33) {
                if(sb.length() > 10) {
                    sb.append(", ");
                }

                sb.append("{id:").append(i$.getMenuId()).append(",name:\"").append(i$.getMenuName()).append("\"").append(",url:\"").append(this.nvl(i$.getLinkUrl())).append("\"").append(",pid:").append(i$.getParentId()).append("}");
            }
        }

        sb.append("],usemu:[");
        int var35 = 0;
        Iterator var36 = var25.iterator();

        while(var36.hasNext()) {
            MenuAuthority m = (MenuAuthority)var36.next();
            if((m.getMenuShow().intValue() & var33) == var33) {
                if(var35++ > 0) {
                    sb.append(", ");
                }

                sb.append("{id:").append(m.getMenuId()).append(",name:\"").append(m.getMenuName()).append("\"").append(",url:\"").append(this.nvl(m.getLinkUrl())).append("\"").append("}");
            }
        }

        sb.append("]}");
        request.getSession().setAttribute("menuAuth", var32);
        this.setQueryShop();
        return this.getResult(true, sb.toString(), "");
    }

    private void setQueryShop() {
        LoginUser user = this.userSession.getLoginUser();
        LinkedHashMap shopMap = new LinkedHashMap();
        DicShopCode shop = user.getLoginShop();
        StringBuilder sb = new StringBuilder();
        sb.append("{shops:[");
        if(shop != null && "01".equals(shop.getShopType())) {
            sb.append("[\"").append(shop.getCode()).append("\"").append(",\"").append(shop.getChName()).append("\"").append(",\"").append(this.nvl(shop.getChPinyin())).append("\"").append("]");
        } else if(shop != null && "02".equals(shop.getShopType())) {
            List shops;
            List areas;
            if(user.getUserInfo().getQueryAllShop().intValue() == 1) {
                shops = this.dicShopCodeDao.getAllShopList();
                areas = this.dicAdmAreaCodeDao.getAllAreaList();
            } else {
                areas = this.sysUserDao.getUserAreaList(user.getUserCode(), false);
                shops = this.sysUserDao.getUserAreaShopList(user.getUserCode());
            }

            List areashops = this.dicAdmAreaCodeDao.getAllAreaShopList();
            int n = 0;
            Iterator areatpMap = shops.iterator();

            while(areatpMap.hasNext()) {
                DicShopCode i$ = (DicShopCode)areatpMap.next();
                if(n++ > 0) {
                    sb.append(",");
                }

                sb.append("[\"").append(i$.getCode()).append("\"").append(",\"").append(i$.getChName()).append("\"").append(",\"").append(this.nvl(i$.getChPinyin())).append("\"").append("]");
                shopMap.put(i$.getCode(), i$.getChName());
            }

            sb.append("],areas:[");
            n = 0;
            LinkedHashMap var15 = new LinkedHashMap();
            Iterator var16 = areas.iterator();

            while(var16.hasNext()) {
                DicAdmAreaCode t = (DicAdmAreaCode)var16.next();
                if(n++ > 0) {
                    sb.append(",");
                }

                sb.append("[\"").append(t.getCode()).append("\"").append(",\"").append(t.getChName()).append("\"").append(",\"").append(t.getAreaType()).append("\"").append(",[");
                int ni = 0;
                Iterator i$1 = areashops.iterator();

                while(i$1.hasNext()) {
                    String[] ss = (String[])i$1.next();
                    if(t.getCode().equalsIgnoreCase(ss[0])) {
                        if(ni++ > 0) {
                            sb.append(",");
                        }

                        sb.append("\"").append(ss[1]).append("\"");
                    }
                }

                sb.append("]]");
                if(!var15.containsKey(t.getAreaType())) {
                    var15.put(t.getAreaType(), this.cache.getValue("admAreaType", t.getAreaType()));
                }
            }

            sb.append("],areatp:[");
            n = 0;

            String var17;
            for(var16 = var15.keySet().iterator(); var16.hasNext(); sb.append("[\"").append(var17).append("\"").append(",\"").append((String)var15.get(var17)).append("\"").append("]")) {
                var17 = (String)var16.next();
                if(n++ > 0) {
                    sb.append(",");
                }
            }
        }

        sb.append("]}");
        user.setShopsJson(sb.toString());
        if(shopMap.size() == 0 && shop != null) {
            shopMap.put(shop.getCode(), shop.getChName());
        }

        user.setShopsMap(shopMap);
    }

    @RequestMapping({"getNewMsg"})
    @ResponseBody
    public String getNewMsg() {
        LoginUser user = this.userSession.getLoginUser();
        return this.getSuccess(String.valueOf(user.getMsgNum()), "");
    }

    @RequestMapping({"chgShop"})
    @ResponseBody
    public String chgShop(@RequestParam("shopCode") String shopCode) {
        DicShopCode shop = (DicShopCode)this.dicShopCodeDao.getById(shopCode);
        if(shop != null && !this.chkNullZ(shop.getStatus())) {
            LoginUser user = this.userSession.getLoginUser();
            if(user.getUserInfo().getLoginAllShop().intValue() != 1 && !this.sysUserDao.chkUserShop(user.getUserCode(), shop.getCode())) {
                return this.getFail(-2, "不能登录到该门店");
            } else {
                user.setLoginShop(shop);
                this.userSession.updateUserMsgNum();
                this.addUserLog("更换登录门店", shop.getCode() + " - " + shop.getChName());
                return this.getResult(true, "");
            }
        } else {
            return this.getFail(-1, "门店错误");
        }
    }

    @RequestMapping({"getUserShop"})
    @ResponseBody
    public String getUserShop() {
        LoginUser user = this.userSession.getLoginUser();
        return this.getSuccess(user.getShopsJson(), "");
    }

    @RequestMapping({"chgpwd"})
    public ModelAndView chgpwd() {
        return new ModelAndView("sys/chgpwd/edit");
    }

    @RequestMapping({"_chgpwd"})
    @ResponseBody
    public String _chgpwd(@RequestParam("oldPwd") String oldPwd, @RequestParam("password") String password) {
        SysUser user = (SysUser)this.sysUserDao.getById(this.getLoginUserCode());
        if(!this.getEncodePwd(user.getUserCode(), oldPwd).equals(user.getPassword())) {
            return this.getResult(false, "旧密码错误");
        } else {
            try {
                this.sysUserDao.updatePassword(user.getUserCode(), this.getEncodePwd(user.getUserCode(), password));
                this.addUserLog("修改密码", "");
            } catch (Exception var5) {
                this.log.error("chgpwd", var5);
                return this.getResult(false, "发生错误");
            }

            return this.getResult(true, "修改成功");
        }
    }
}